/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smart.service;

import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Test;

/**
 *
 * @author arthur
 */
public class LoaderData {

    protected static final Log LOGGER = LogFactory.getLog(LoaderData.class);
    
    private static IDatabaseTester databaseTester = null;
    
    public static String path = "src"+ File.separatorChar +"test"+ File.separatorChar +"com"+ File.separatorChar +"smart"+ File.separatorChar +"service" + File.separatorChar;

   

    @Test
	public void pathTest(){
		try{
			
			//path = "SmartWebService\\src\\test\\";
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

    public LoaderData() {
        
    }

    
	public static void init(String nome) throws Exception {
		/* /# INICIO - EXERC. 2 */
		if (databaseTester == null) {
			databaseTester = new JdbcDatabaseTester("org.hsqldb.jdbcDriver", "jdbc:hsqldb:hsql://127.0.0.1:55000", "sa", "");
		}
		IDatabaseConnection connection = null;
		IDataSet dataSet = null;
		try {
			connection = databaseTester.getConnection();
			dataSet = getDataSet(nome);
			LOGGER.info("APAGANDO DADOS DA BASE DE DADOS");
			LOGGER.info("CARREGANDO DO DADOS DE TESTE NA BASE DE DADOS");
			DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
			LOGGER.info("DADOS DE TESTE CARREGADOS NA BASE DE DADOS");
		} catch (Exception ex) {
			LOGGER.error("ERRO CARREGAMENTO DE DADOS", ex);
			throw ex;
		} finally {
			try {
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException ex) {
				LOGGER.error(ex);
			}
		}
		LOGGER.info("DADOS DE TESTE CARREGADOS");

		// FIM - EXERC. 2 #*/
	}

    
    public static void end(String nome) throws Exception {
    	/*/# INICIO - EXERC. 2*/
    	if (databaseTester == null) {
			databaseTester = new JdbcDatabaseTester("org.hsqldb.jdbcDriver", "jdbc:hsqldb:hsql://127.0.0.1:55000", "sa", "");
		}
		IDatabaseConnection connection = null;
		IDataSet dataSet = null;
		try {
			connection = databaseTester.getConnection();
			dataSet = getDataSet(nome);
			DatabaseOperation.DELETE_ALL.execute(connection, dataSet);
			LOGGER.info("DADOS DA BASE APAGADOS");
		} catch(Exception ex){
			LOGGER.error("ERRO CARREGAMENTO DE DADOS", ex);
			throw ex;
		}finally{
			try {
				if(connection != null ){
				connection.close();
				}
			} catch (SQLException ex) {
				LOGGER.error(ex);
			}
		}
		LOGGER.info("DADOS DE TESTE CARREGADOS");
    	// FIM - EXERC. 2 #*/
    }

     protected static IDataSet getDataSet(String nome) throws Exception
    {
         File file = new File(path + nome);
         if(file.exists()){
            return new FlatXmlDataSet(new FileInputStream(file));
         }else{
        	file = new File("SmartWebService" + File.separatorChar + path + nome);
        	if(file.exists()){
        		return new FlatXmlDataSet(new FileInputStream(file));
        	}else{
        		throw new RuntimeException("O arquivo " + nome + " nao existe");	
        	}
         }
    }

}
